package edu.ucsc.gameAI;

import java.awt.Color;

import pacman.game.Constants.DM;
import pacman.game.Constants.GHOST;
import pacman.game.Constants.MOVE;
import pacman.game.Game;
import pacman.game.GameView;
//import edu.ucsc.gameAI.Debug.MESSAGETYPE;
import edu.ucsc.gameAI.decisionTrees.binary.IBinaryNode;

public class MoveTowardsClosestPowerPillAction implements IAction, IBinaryNode {

	GHOST closestGhost;
	int safeDistance = 10;

	public void doAction() {
		/*
		 * Debug.debug("MoveTowardsClosestActivePowerPill", MESSAGETYPE.ACTION);
		 * 
		 * int pacManCurrentNode = game.getPacmanCurrentNodeIndex(); int[]
		 * activePowerPills = game.getActivePowerPillsIndices(); int
		 * closestPowerPillIndex =
		 * game.getClosestNodeIndexFromNodeIndex(pacManCurrentNode,
		 * activePowerPills, DM.PATH);
		 * 
		 * return game.getNextMoveTowardsTarget(pacManCurrentNode,
		 * closestPowerPillIndex, DM.PATH);
		 */
	}

	public MOVE getMove() {
		// TODO Auto-generated method stub
		return MOVE.RIGHT;
	}

	public MOVE getMove(Game game) {
		 Debug.debug("MoveTowardsClosestActivePowerPill", Debug.MESSAGETYPE.ACTION);
		//System.out.println("Inside of MoveTowardsClosestPowerPill.java!");

		int pacmanIndex = game.getPacmanCurrentNodeIndex();
		// default to the first active power pill index

		int moveToIndex = -1;
		boolean isGhostInTheWay;

		// Loop through all available power pills, to find the next one to
		// eat.
		// The next one to eat should ideally NOT make Pacman cross through
		// a ghost
		// This is to preserve Pacman's life!
		for (int powerPillIndex : game.getActivePowerPillsIndices()) {
			isGhostInTheWay = false;
			// powerPillIndex represents a power pill. Let's grab the shortest
			// path to it.


			for (GHOST ghostType : GHOST.values()) {
				
				if(game.getGhostLairTime(ghostType) > 0)
					continue;

				// Great, now we have the shortest path, let's make sure that it
				// doesn't include a Ghost.
				int[] pathToThisPillIndex = game.getShortestPath(pacmanIndex,
						powerPillIndex);
				int ghostIndex = game.getGhostCurrentNodeIndex(ghostType);

				for (int i = 0; i < pathToThisPillIndex.length; i++) {
					if (pathToThisPillIndex[i] == ghostIndex) {
						if (game.getShortestPathDistance(pacmanIndex, ghostIndex) < safeDistance) {
							// Ghost in the way!
							Debug.debug("The path to the pill has a dangerous ghost on it (" + ghostType.name() + ")", Debug.MESSAGETYPE.STATEMENT);
							isGhostInTheWay = true;
							break;
						}
					}
				}

				int[] ghostPathToThisPillIndex = game.getShortestPath(ghostIndex, powerPillIndex);
				
				int ghostDistance = game.getShortestPathDistance(ghostIndex, powerPillIndex, game.getGhostLastMoveMade(ghostType));
				int pacmanDistance = game.getShortestPathDistance(pacmanIndex, powerPillIndex);
				
				//is Pacman's path to the power pill longer than the current ghost's?
				if (ghostDistance <= pacmanDistance) {
					Debug.debug("Pacman's path is greater than the ghost's (" + ghostType.name() + ")" , Debug.MESSAGETYPE.ACTION);
					int xCoordinate = game.getNodeXCood(ghostIndex);
					int yCoordinate = game.getNodeYCood(ghostIndex);
					boolean checkingX = true;
					
					switch(game.getGhostLastMoveMade(ghostType))
					{
					case DOWN:
						yCoordinate -= 1;
						checkingX = false;
						break;
					case UP:
						yCoordinate += 1;
						checkingX = false;
						break;
					case LEFT:
						xCoordinate -= 1;
						break;
					default:
						xCoordinate += 1;
						break;
					}
					
					//Make sure the ghost isn't on a path to the power pill as well
					for (int i = 0; i < ghostPathToThisPillIndex.length; i++) {
						if (checkingX)
							if (game.getNodeXCood(ghostPathToThisPillIndex[i]) == xCoordinate)
							{
								isGhostInTheWay = true;
								Debug.debug("The ghost " + ghostType.name() + " appears to be on a X path toward the same power pill", Debug.MESSAGETYPE.STATEMENT);
								break;
							}

						else		
							if (game.getNodeYCood(ghostPathToThisPillIndex[i]) == yCoordinate)
							{
								isGhostInTheWay = true;
								Debug.debug("The ghost appears to be on a Y path toward the same power pill", Debug.MESSAGETYPE.STATEMENT);
								break;
							}					}
				}

				if (isGhostInTheWay)
					continue;
				else
					moveToIndex = powerPillIndex;
			}
		}

		if (moveToIndex != -1){
			int[] powerPillPath = game.getShortestPath(pacmanIndex, moveToIndex);
			GameView.addPoints(game, Color.BLUE, powerPillPath );
			GameView.addPoints(game, Color.RED, moveToIndex);
			return game.getNextMoveTowardsTarget(pacmanIndex, moveToIndex,
					DM.PATH);
		}
		else
		{
			Debug.debug("No safe path, RUN!!", Debug.MESSAGETYPE.STATEMENT);
			return new RunAwayFromGhosts().getMove(game);
		}

	}

	public IAction makeDecision(Game game) {
		return this;
	}
}
